package com.example.day01_weather.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day01_weather.R;
import com.example.day01_weather.adapter.IaqiAdapter;
import com.example.day01_weather.adapter.Station_List_City_Adapter;
import com.example.day01_weather.adapter.Station_listAdapter;
import com.example.day01_weather.adapter.WeatherAdapter;
import com.example.day01_weather.api.ApiService;
import com.example.day01_weather.bean.Station_iaqiBean;
import com.example.day01_weather.bean.Station_listBean;
import com.example.day01_weather.net.HttpManger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class TemperatureActivity extends AppCompatActivity {

    @BindView(R.id.btn_city)
    Button btnCity;
    @BindView(R.id.btn_town)
    Button btnTown;
    @BindView(R.id.rv)
    RecyclerView rv;
    ArrayList<Station_listBean.DataBean> list = new ArrayList<>();
    ArrayList<Station_listBean.DataBean> oldList = new ArrayList<>();
    List<Station_listBean.DataBean> dataBeans ;
    private String city_code;
    private ArrayList<Station_listBean.DataBean> newList;
    private String station_code="";
    private ArrayList<Station_iaqiBean> iaqiList;
    private IaqiAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temperature);
        ButterKnife.bind(this);
        initView();
        initData();
        initIaqiData("2269A");
    }

    private void initIaqiData(String str) {
        ApiService lanApiService = HttpManger.getInstance().getLanApiService();
        lanApiService.getStation_Iaqi(str)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Station_iaqiBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Station_iaqiBean station_iaqiBean) {
                        if(iaqiList!=null){
                            iaqiList.clear();
                        }
                        iaqiList.add(station_iaqiBean);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TAG", "onError: "+e.getMessage() );
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initData() {


        ApiService lanApiService = HttpManger.getInstance().getLanApiService();
        lanApiService.getStation_List()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Station_listBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Station_listBean station_listBean) {
                        List<Station_listBean.DataBean> data = station_listBean.getData();
                        btnCity.setText(data.get(0).getCity());
                        btnTown.setText(data.get(0).getStation());
                        station_code=data.get(0).getStation_code();
                        city_code=data.get(0).getCity_code();
                        list.addAll(data);
                        oldList.addAll(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TAG", "onError: " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    private void initView() {
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        iaqiList = new ArrayList<>();

        adapter = new IaqiAdapter(iaqiList, this);
        rv.setAdapter(adapter);

    }

    @OnClick({R.id.btn_city, R.id.btn_town})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_city:
                cityPop();
                break;
            case R.id.btn_town:
                pop();
                break;
        }
    }

    private void pop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop, null);
        PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        RecyclerView rv = view.findViewById(R.id.popp);
        rv.setLayoutManager(new LinearLayoutManager(this));

        if(city_code==null){
            Toast.makeText(this, "请先选择您所在的城市呢", Toast.LENGTH_SHORT).show();
            pop.dismiss();
        }else{
            newList = new ArrayList<>();
            for (int i = 0; i < oldList.size(); i++) {
                if(oldList.get(i).getCity_code().equals(city_code)){
                    newList.add(oldList.get(i));
                }
            }
            Station_List_City_Adapter adapter = new Station_List_City_Adapter(newList, this);
            Log.e("TAG", "pop: "+newList.size());
            rv.setAdapter(adapter);
            adapter.notifyDataSetChanged();

            adapter.setOnItemClickListener(new Station_listAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {

                    btnTown.setText(newList.get(position).getStation());
                    initIaqiData(newList.get(position).getStation_code());
                    pop.dismiss();
                }
            });
        }


        pop.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    private void cityPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.pop, null);
        PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        RecyclerView rv = view.findViewById(R.id.popp);
        rv.setLayoutManager(new LinearLayoutManager(this));
        HashMap<String, List<Station_listBean.DataBean>> map = new HashMap<>();


        for (int i = 0; i < list.size(); i++) {
            String city_code = list.get(i).getCity_code();
            if(map.get(city_code)==null){
                dataBeans = new ArrayList<>();
            }else{
                dataBeans=map.get(city_code);
            }
            dataBeans.add(list.get(i));

            map.put(city_code,dataBeans);
        }
        list.clear();
        for (Map.Entry<String,List<Station_listBean.DataBean>> entry: map.entrySet()){
            Log.e("TAG", "pop: "+entry.getKey()+"//"+entry.getValue());
            list.add(entry.getValue().get(0));
        }

        Station_listAdapter adapter = new Station_listAdapter(list, this);
        Log.e("TAG", "pop: "+list.size());
        rv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(new Station_listAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                city_code = list.get(position).getCity_code();
                btnCity.setText(list.get(position).getCity());
                pop.dismiss();
            }
        });


        pop.showAtLocation(view, Gravity.CENTER, 0, 0);
    }
}